using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 飞碟获取规则
public struct Ruler {
    public int trialNum; 
    public int roundNum; 
    public int roundSum; 
    public int [] roundDisksNum; 
    public float sendTime; 

    public int size; 
    public int speed; 
    public string color; 
    public Vector3 direction; 
    public Vector3 beginPos; 
};

public class RoundController : MonoBehaviour {
    private IActionManager actionManager; 
    private ScoreRecorder scoreRecorder; 
    private MainController mainController;
    private Ruler ruler; 

    void Start() {
        actionManager = gameObject.AddComponent<CCActionManager>();
        gameObject.AddComponent<PhysisActionManager>();
        scoreRecorder = new ScoreRecorder();
        mainController = Director.GetInstance().mainController;
        gameObject.AddComponent<DiskFactory>();
        InitRuler();
    }

    void InitRuler() {
        ruler.trialNum = 0;
        ruler.roundNum = 0;
        ruler.sendTime = 0;
        ruler.roundDisksNum = new int [10];
        generateRoundDisksNum();
    }

    // 生成每trial同时发出的飞碟数量的数组，同时发出飞碟个数不超过4
    public void generateRoundDisksNum() {
        for (int i = 0; i < 10; ++i) {
            ruler.roundDisksNum[i] = Random.Range(0, 4) + 1;
        }
    }

    public void Reset() {
        InitRuler();
        scoreRecorder.Reset();
    }

    public void Record(DiskData disk) {
        scoreRecorder.Record(disk);
    }

    public int GetScores() {
        return scoreRecorder.score;
    }

    public void SetRoundSum(int roundSum) {
        ruler.roundSum = roundSum;
    }

    // 设置游戏模式，同时支持物理运动模式和动力学运动模式
    public void SetPlayDiskModeToPhysis(bool isPhysis) {
        if (isPhysis) {
            actionManager = Singleton<PhysisActionManager>.Instance as IActionManager;
        }
        else {
            actionManager = Singleton<CCActionManager>.Instance as IActionManager;
        }
    }

    // 发射飞碟
    public void LaunchDisk() {
        int [] beginPosY = new int [4]{0, 0, 0, 0};

        for (int i = 0; i < ruler.roundDisksNum[ruler.trialNum]; ++i) {
            int randomNum = Random.Range(0, 3) + 1;
            ruler.speed = randomNum * (ruler.roundNum + 4);

            randomNum = Random.Range(0, 3) + 1;
            if (randomNum == 1) {
                ruler.color = "red";
            }
            else if (randomNum == 2) {
                ruler.color = "green";
            }
            else {
                ruler.color = "blue";
            }

            ruler.size = Random.Range(0, 3) + 1;

            randomNum = Random.Range(0, 2);
            if (randomNum == 1) {
                ruler.direction = new Vector3(3, 1, 0);
            }
            else {
                ruler.direction = new Vector3(-3, 1, 0);
            }

            do {
                randomNum = Random.Range(0, 4);
            } while (beginPosY[randomNum] != 0);
            beginPosY[randomNum] = 1;
            ruler.beginPos = new Vector3(-ruler.direction.x * 4, -0.5f * randomNum, 0);

            GameObject disk = Singleton<DiskFactory>.Instance.GetDisk(ruler);
        
            actionManager.PlayDisk(disk, ruler.speed, ruler.direction);
        }
    }

    // 释放工厂飞碟
    public void FreeFactoryDisk(GameObject disk) {
        Singleton<DiskFactory>.Instance.FreeDisk(disk);
    }

    // 释放所有工厂飞碟
    public void FreeAllFactoryDisk() {
        GameObject[] obj = FindObjectsOfType(typeof(GameObject)) as GameObject[];
        foreach (GameObject g in obj) {
            if (g.gameObject.name == "Disk(Clone)(Clone)") {
                Singleton<DiskFactory>.Instance.FreeDisk(g);
            }
        }
    }

    void Update() {
        if (mainController.GetGameState() == (int)GameState.Playing) {
            ruler.sendTime += Time.deltaTime;
            if (ruler.sendTime > 2) {
                ruler.sendTime = 0;
                if (ruler.roundSum == -1 || ruler.roundNum < ruler.roundSum) {
                    mainController.SetViewTip("");
                    LaunchDisk();
                    ruler.trialNum++;
                    if (ruler.trialNum == 10) {
                        ruler.trialNum = 0;
                        ruler.roundNum++;
                        generateRoundDisksNum();
                    }
                }
                else {
                    mainController.SetViewTip("Click Restart and Play Again!");
                    mainController.SetGameState((int)GameState.GameOver);
                }
                if (ruler.trialNum == 0) mainController.SetViewRoundNum(ruler.roundNum);
                else mainController.SetViewRoundNum(ruler.roundNum + 1);
                mainController.SetViewTrialNum(ruler.trialNum);
            }
        }
    }
}